/**
YASTACS single user game server module.

License: GNU General Public License

YASTACS game
Copyright (C) 2012 Kalman Kiss, kiskami@freemail.hu

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Contact author at: kiskami@freemail.hu or
Hungary, 8900 Zalaegerszeg
Kakukkfu u 4.
*/
module server.SingleUserGameServer;

import Common;
import iCore;

import server.GameServerInterface;

import ugen.UniversumGenerator;

import std.stdio;

immutable string SESSION_ID = "single_user_session";

immutable string CONNECTED_MESSAGE = "Connected to server.";
immutable string DISCONNECTED_MESSAGE = "Disconnected from server.";
immutable string NEWGAMESTARTED_MESSAGE = "New game started on server.";
immutable string GAMELOADEDSTARTED_MESSAGE = "Saved game loaded and started.";
immutable string GAMEALREADYSTARTED_MESSAGE = "Game already started";

/**
* Egyjátékos játék szerver.
*/
class SingleUserGameServer : GameServerInterface {
	this(iCore core_) {
		debug writeln("SingleUserGameServer ctor");
		core = core_;
	}

	void setProgressDelegate(progressdelegate pd_) { pd = pd_; }
	void setLogDelegate(logdelegate ld_) { ld = ld_; }

	ServerResult connectToServer(string servername_, string loginname_, string password_) {
		debug writeln("SingleUserGameServer: client connected");
		ld("SingleUserGameServer: client connected");
		client_connected = true;
		return ServerResult(ServerResultCode.OK, SESSION_ID, CONNECTED_MESSAGE, result_sequence++);
	}

	ServerResult disconnectFromServer(SessionId session_id_) {
		debug writeln("SingleUserGameServer: client disconnected");
		ld("SingleUserGameServer: client disconnected");

		if(!client_connected) {
			return ServerResult(ServerResultCode.GENERAL_ERROR, SESSION_ID, "Client already disconnected!", result_sequence++);
		}

		client_connected = false;

		// TODO
		stop_game();

		return ServerResult(ServerResultCode.OK, SESSION_ID, DISCONNECTED_MESSAGE, result_sequence++);
	}

	ServerResult startNewGame(SessionId session_id_, string name_, string seed_, GameDifficultyLevel level_, bool violence_) {

		if(game_started) {
			return ServerResult(ServerResultCode.GENERAL_ERROR, SESSION_ID, GAMEALREADYSTARTED_MESSAGE, result_sequence++);
		}

	    name = name_;
		seed = seed_;
		level = level_;
		violence = violence_;

		debug writeln("SingleUserGameServer: starting new game, name=", name
					  , ", seed=", seed
					  , ", level=", level
					  , ", violence=", violence);
//		ld("SingleUserGameServer: starting new game, name=" ~ name);

		ug = new UniversumGenerator(seed);
		ug.setProgressDelegate(pd);
		ug.setLogDelegate(ld);
		ld("SingleUserGameServer: generating new universum with seed=" ~ seed);
		ug.gen();
		debug writeln("SingleUserGameServer: universum generated");

		// TODO
		start_game();


		return ServerResult(ServerResultCode.OK, SESSION_ID, NEWGAMESTARTED_MESSAGE, result_sequence++);
	}

	ServerResult loadAndStartGame(string name) {
		debug writeln("SingleUserGameServer: loading and starting game");

		if(game_started) {
			return ServerResult(ServerResultCode.GENERAL_ERROR, SESSION_ID, GAMEALREADYSTARTED_MESSAGE, result_sequence++);
		}

		// TODO


		return ServerResult(ServerResultCode.NOTSUPPORTED, SESSION_ID, "Loading a game not yet supported.", result_sequence++);
	}

	/// update single user game state
	ServerResult update(SessionId session_id_, real elapsedTime) {
	//TODO
		return ServerResult(ServerResultCode.OK, SESSION_ID, "", result_sequence++);
	}

	ServerResult getUniversumData(SessionId session_id_) {
		debug writeln("SingleUserGameServer: sending universum data to client");
		return ServerResult(ServerResultCode.OK, SESSION_ID, ug.universum.toJson(), result_sequence++);
	}

	ServerResult getPlayerSpawnData(SessionId session_id_) {
		debug writeln("SingleUserGameServer: sending playerspawn data to client");
		// TODO
		return ServerResult(ServerResultCode.OK, SESSION_ID, "", result_sequence++);
	}

private:

	void start_game() {

		game_started = true;
	}

	void stop_game() {


		game_started = false;
	}


	string name;
	string seed;
	GameDifficultyLevel level;
	bool violence;

	SequenceType result_sequence;

	iCore core;
	UniversumGenerator ug;

	progressdelegate pd;
	logdelegate ld;

	bool client_connected
		, game_started;

}
